//! Simple HTTP Protocol Test
//! 
//! This example tests basic HTTP protocol functionality.

use ai_test_platform::{
    protocols::{http::HttpProtocolHandler, ProtocolHandler},
    models::step::StepContext,
};
use serde_json::json;
use std::collections::HashMap;
use uuid::Uuid;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Simple HTTP Protocol Test ===");
    
    // Create HTTP protocol handler
    let mut handler = HttpProtocolHandler::new();
    println!("✓ HTTP protocol handler created");
    
    // Test protocol name
    assert_eq!(handler.protocol_name(), "http");
    println!("✓ Protocol name: {}", handler.protocol_name());
    
    // Test supported operations
    let operations = handler.supported_operations();
    println!("✓ Supported operations: {:?}", operations);
    assert!(operations.contains(&"get".to_string()));
    assert!(operations.contains(&"post".to_string()));
    
    // Initialize with basic configuration
    let config = HashMap::new();
    handler.initialize(config).await?;
    println!("✓ HTTP protocol handler initialized");
    
    // Test basic GET request (using a reliable test endpoint)
    println!("\n=== Testing GET Request ===");
    
    let mut get_params = HashMap::new();
    get_params.insert("url".to_string(), json!("https://httpbin.org/get"));
    get_params.insert("headers".to_string(), json!({
        "User-Agent": "AI-Test-Platform/1.0"
    }));
    
    let mut context = StepContext::new(Uuid::new_v4(), Uuid::new_v4());
    
    match handler.execute("get", get_params, &mut context).await {
        Ok(result) => {
            println!("GET Request Result:");
            println!("  Success: {}", result.success);
            println!("  Elapsed: {}ms", result.elapsed);
            
            if let Some(http_stat) = &result.http_stat {
                println!("  Status Code: {}", http_stat.get("status_code").unwrap_or(&0));
            }
            
            if result.success {
                println!("✓ GET request successful");
            } else {
                println!("⚠ GET request failed but handled gracefully");
            }
        }
        Err(e) => {
            println!("✗ GET request error: {}", e);
            return Err(e.into());
        }
    }
    
    // Test parameter validation
    println!("\n=== Testing Parameter Validation ===");
    
    let mut valid_params = HashMap::new();
    valid_params.insert("url".to_string(), json!("https://example.com"));
    
    match handler.validate_params("get", &valid_params) {
        Ok(_) => println!("✓ Valid parameters accepted"),
        Err(e) => {
            println!("✗ Valid parameters rejected: {}", e);
            return Err(e.into());
        }
    }
    
    // Test invalid parameters
    let empty_params = HashMap::new();
    match handler.validate_params("get", &empty_params) {
        Ok(_) => println!("⚠ Empty parameters unexpectedly accepted"),
        Err(_) => println!("✓ Empty parameters correctly rejected"),
    }
    
    println!("\n=== Test Completed Successfully ===");
    println!("HTTP protocol handler is working correctly!");
    
    Ok(())
}
